<template>
  <div class="login-container">
    <div class="login-bg"></div>
    <div class="login-form">
      <div class="login-logo">
        <h2>后台管理系统</h2>
        <p>Enterprise Resource Planning</p>
      </div>
      <el-form ref="loginForm" :model="loginData" :rules="loginRules">
        <el-form-item prop="username">
          <el-input
            v-model="loginData.username"
            placeholder="请输入用户名"
            prefix-icon="User"
            size="large"
            class="login-input"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginData.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            show-password
            size="large"
            class="login-input"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            size="large"
            style="width: 100%"
            :loading="loading"
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const loginForm = ref(null)
const router = useRouter()
const loading = ref(false)

const loginData = reactive({
  username: '',
  password: ''
})

const loginRules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ]
}

const handleLogin = () => {
  loginForm.value.validate(valid => {
    if (valid) {
      loading.value = true
      // 模拟登录请求
      setTimeout(() => {
        // 这里应该是调用登录接口
        // 暂时写死用户名密码 admin/123456
        if (loginData.username === 'admin' && loginData.password === '123456') {
          ElMessage.success('登录成功')
          router.push('/dashboard')
        } else {
          ElMessage.error('用户名或密码错误')
        }
        loading.value = false
      }, 1000)
    } else {
      ElMessage.error('请填写正确的登录信息')
    }
  })
}
</script>

<style lang="scss" scoped>
.login-container {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  position: relative;
  overflow: hidden;

  .login-bg {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: url('@/assets/login-bg.svg') no-repeat center center;
    background-size: cover;
    z-index: 1;
  }

  .login-form {
    width: 400px;
    padding: 40px;
    background: rgba(255, 255, 255, 0.95);
    border-radius: 10px;
    box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
    backdrop-filter: blur(10px);
    z-index: 2;
    position: relative;

    .login-logo {
      text-align: center;
      margin-bottom: 30px;

      h2 {
        font-size: 28px;
        color: #333;
        margin-bottom: 10px;
        font-weight: 600;
      }

      p {
        font-size: 14px;
        color: #666;
      }
    }

    .login-input {
      :deep(.el-input__wrapper) {
        background: rgba(255, 255, 255, 0.8);
        box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
      }
    }
  }

  @media (max-width: 768px) {
    .login-form {
      width: 90%;
      margin: 0 15px;
      padding: 30px 20px;
    }
  }
}
</style>
